#!/bin/bash
#
# Lists and summarizes the agents found in the Pulley agents directory.
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 18 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## p_cmd_list_agents_help
##
## Echoes help information for the list_agents command. Returns 0.
##
function p_cmd_list_agents_help() {
	cat << EOF
Usage: pulley list_agents [--short]

Lists and summarizes the agents found in the Pulley agents directory, along
with any agents that are added by the site configuration. Exits with a status
of 0 in the normal case (non-zero if errors occur).

If the optional --short argument is given, displays a plain list of the
agents without formatting or summaries.
EOF
	return 0
}


## p_cmd_list_agents_summary
##
## Echoes summary information for the list_agents command. Returns 0.
##
function p_cmd_list_agents_summary() {
	echo "Lists and summarizes available agents"
	return 0
}


## p_cmd_list_agents
##
## Implements the list_agents command, listing and optionally summarizing
## each agent found. Returns 0 in the normal case, non-zero if an error
## occurs.
##
function p_cmd_list_agents() {
	local status=0
	local chk=0
	local name=
	
	# Load all available agents
	for agent in $(ls "${P_LIBDIR}/agents"/*.agent); do
		name=$(basename "${agent}" | sed s/\.agent$//)
		enable_agent "${name}"
		chk=$?
		[ ${status} -eq 0 ] && status=${chk}
	done
	
	if [[ ${status} -eq 0 ]]; then
		if [[ "$1" != "--short" ]]; then
			echo "Available Agents"
			echo "----------------"
		fi
		
		for agent in "${P_AGENTS[@]}"; do
			if [[ "$1" != "--short" ]]; then
				echo
				echo "${agent}"
				echo "  $(agent_summary ${agent})"
			else
				echo "${agent}"
			fi
		done
	fi
	
	return ${status}
}
